package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.zlx.module_base.base_api.res_data.AuctionFriendBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.AuctionDetailsAc;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.xiandan.countdowntimer.CountDownTimerSupport;
import in.xiandan.countdowntimer.OnCountDownTimerListener;

/**
 * 拍卖大厅
 */
public class AuctionHallFg extends BaseFg {

    @BindView(R.id.sw_layout)
    SwipeRefreshLayout sw_layout;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<AuctionFriendBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<AuctionFriendBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fg_auction_hall;
    }


    @Override
    protected void initViews() {
        super.initViews();

        getAuctionFriendList();
        initRecyclerView();

        sw_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getAuctionFriendList();
            }
        });
    }

    /**
     * 拍卖大厅列表
     */
    private void getAuctionFriendList() {
        ApiUtil.getFriendApi().getAuctionFriendList(SPUtil.getToken(getContext()))
                .observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AuctionFriendBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<AuctionFriendBean> data) {
                        sw_layout.setRefreshing(false);
                        mList.clear();
                        if (null != data) {
                            if (null != data.getData()) {
                                if (null != data.getData().getInfo()) {
                                    if (data.getData().getInfo().size() > 0) {
                                        if (null != data.getData().getInfo().get(0).getRecords()) {
                                            mList.addAll(data.getData().getInfo().get(0).getRecords());
                                        }
                                    }
                                }
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    /**
     * 初始化数据
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new CommonRecyclerViewAdapter<AuctionFriendBean.InfoDTO.RecordsDTO>(getContext(), mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, AuctionFriendBean.InfoDTO.RecordsDTO entity, int position) {
                GlideImgManager.glideLoader(getContext(), entity.getAvatar(), holder.getView(R.id.iv_recommend_head), 0);
                holder.setText(R.id.tv_recommend_nickname, entity.getNickName());
                holder.setText(R.id.tv_up_phone, entity.getUphone());
                holder.setText(R.id.tv_phone, entity.getFriendPhone());
                holder.setText(R.id.tv_time2, entity.getCreateTime());
                holder.setText(R.id.tv_money, "￥" + entity.getBigMoney());
                setTime(holder.getView(R.id.tv_time), Long.valueOf(entity.getTime()) * 1000);
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_auction_hall;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                startActivity(new Intent(getContext(), AuctionDetailsAc.class)
                        .putExtra("id", mList.get(position).getId())
                        .putExtra("type", "1"));
            }
        });
    }

    /**
     * 倒计时
     */
    private void setTime(TextView tv_time, long time) {
        //毫秒
        CountDownTimerSupport mTimer = new CountDownTimerSupport(time, 100);
        mTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                // 倒计时间隔
                tv_time.setText(formatDuring(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                // 倒计时结束
                tv_time.setText("倒计时结束");
            }

            @Override
            public void onCancel() {
                // 倒计时手动停止
            }
        });
        mTimer.start();
    }

    public static String formatDuring(long mss) {

        long days = mss / (1000 * 60 * 60 * 24);

        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);

        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);

        long seconds = (mss % (1000 * 60)) / 1000;

        return days + " 天 " + hours + ":" + minutes + ":"

                + seconds;

    }

}
